package com.qianyu.project.web.controller.mfy.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qianyu.project.entity.MaterialClass;
import com.qianyu.project.service.IMaterialClassService;
import com.qianyu.project.service.impl.MaterialClassServiceImpl;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author mfy
 * @desc
 */
public class MaterialClassUtils {


    public static List<MaterialClassBean> TreeRendering(IMaterialClassService materialClassService){
        //拿到所有的父节点
        List<MaterialClassBean> treeList = materialClassService.getTreeNode();
        //定义集合保存要返回的数据结构
        List<MaterialClassBean> rootList = Lists.newArrayList();
        //设置第一层节点
        treeList.iterator().forEachRemaining(treeNode -> {
            if(treeNode.getParentid()==null){
                rootList.add(treeNode);
            }
        });
        //遍历顶层节点设置第二层第三层节点
        rootList.iterator().forEachRemaining(root-> {
            List<MaterialClassBean> menuList = Lists.newArrayList();
            treeList.iterator().forEachRemaining(treeNode -> {
                if(root.getId().equals(treeNode.getParentid())){
                    menuList.add(treeNode);
                }
            });
            menuList.iterator().forEachRemaining(menu -> {
                List<MaterialClassBean> childList = Lists.newArrayList();
                treeList.iterator().forEachRemaining(treeNode -> {
                    if(menu.getId().equals(treeNode.getParentid())){
                        childList.add(treeNode);
                    }
                });
                menu.setChildren(childList);
            });
            root.setChildren(menuList);
        });
        //返回数据
        return rootList;
    }
}
